package com.csd.serviceImpl;

import java.util.List;

import com.csd.dao.CostRecordDAO;
import com.csd.dao.ProjectDAO;
import com.csd.exception.DeleteException;
import com.csd.exception.GetException;
import com.csd.exception.SaveException;
import com.csd.exception.UpdateException;
import com.csd.helper.SessionManager;
import com.csd.pojo.CostRecord;
import com.csd.pojo.Project;
import com.csd.service.ICostRecordService;

/**
 * 
 * Service：费用记录
 * 
 * @author csd
 * @version 1.0
 * 
 * <br>
 *          修订时间：2009-12-17 <br>
 *          修改原因：创建文件 <br>
 *          修改人：何文深
 * 
 * <br>
 * 
 */
public class CostRecordService implements ICostRecordService {
	private CostRecordDAO costRecordDAO;

	private ProjectDAO projectDAO;

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public CostRecordDAO getCostRecordDAO() {
		return costRecordDAO;
	}

	public void setCostRecordDAO(CostRecordDAO costRecordDAO) {
		this.costRecordDAO = costRecordDAO;
	}

	/**
	 * 删除费用记录
	 * 
	 * @param id
	 * @return boolean
	 */
	public boolean deleteCostRecord(Integer id) {
		CostRecord costRecord = null;
		costRecord = costRecordDAO.findById(id);
		try {
			costRecordDAO.delete(costRecord);
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除费用记录异常");
		}
		return true;
	}

	/**
	 * 根据ID查询费用记录
	 * 
	 * @param id
	 * @return CostRecord
	 */
	public CostRecord getCostRecordByID(Integer id) {
		CostRecord costRecord = null;
		try {
			costRecord = costRecordDAO.findById(id);

		} catch (RuntimeException re) {
			throw new GetException(re, "查询费用记录异常");
		}
		return costRecord;
	}

	/**
	 * 通过项目ID获得花费记录
	 * 
	 * @param id
	 * @return List<CostRecord>
	 * 
	 */
	public List<CostRecord> listCostRecord(Integer id) {
		List<CostRecord> costRecords = null;
		try {
			costRecords = costRecordDAO.findByProperty("project.id", id);
		} catch (RuntimeException re) {
			throw new GetException(re, "列出费用记录异常");
		}
		return costRecords;
	}

	/**
	 * 保存项目-费用记录
	 * 
	 * @param costRecord
	 * @return boolean
	 * 
	 */
	public boolean saveCostRecord(CostRecord costRecord) {
		boolean tag = false;
		try {
			int id = SessionManager.getUserID();
			List list = projectDAO.findByTeam(id);
			if (list != null && list.size() > 0) {
				costRecord.setProject((Project) list.get(0));
				costRecordDAO.save(costRecord);
				tag = true;
			}

		} catch (RuntimeException re) {
			throw new SaveException(re, "费用记录提交异常");
		}
		return tag;
	}

	/**
	 * 查询团队费用记录
	 * 
	 * @param
	 * @return List<CostRecord>
	 * 
	 */
	public List<CostRecord> listCostRecordByTeam() {
		List<CostRecord> list = null;

		try {
			int id = SessionManager.getUserID();
			list = costRecordDAO.findByProperty("project.team.id", id);

		} catch (RuntimeException re) {
			throw new GetException(re, "列出团队提交的费用记录异常");
		}

		return list;
	}

	/**
	 * 更新费用记录
	 * 
	 * @param costRecord
	 * @return boolean
	 * 
	 */
	public boolean updateCostRecpord(CostRecord costRecord) {
		try {
			costRecordDAO.updateCostRecpord(costRecord);
		} catch (RuntimeException re) {
			throw new UpdateException(re, "更新费用记录异常");
		}
		return true;
	}

	/**
	 * 查询工程的费用记录
	 * 
	 * @param projectID
	 * @return List<CostRecord>
	 * 
	 */
	public List<CostRecord> listCostRecordByProject(Integer projectID) {
		List<CostRecord> list = null;

		try {
			list = costRecordDAO.findByProperty("project.id", projectID);
		} catch (RuntimeException re) {
			throw new GetException(re, "列出工程费用记录异常");
		}

		return list;
	}

}
